package org.jeecg.modules.demo.mall.yxgrowthvalue.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.jeecg.modules.demo.mall.entity.YxGrowthValue;
import org.jeecg.modules.demo.mall.yxgrowthvalue.mapper.YxGrowthValueMapper;
import org.jeecg.modules.demo.mall.yxgrowthvalue.service.IYxGrowthValueService;
import org.jeecg.modules.demo.mall.yxuser.service.IYxUserService;
import org.jeecg.modules.demo.weixin.dto.PayDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description: 成长值表
 * @Author: jeecg-boot
 * @Date: 2023-11-08
 * @Version: V1.0
 */
@Service
public class YxGrowthValueServiceImpl extends ServiceImpl<YxGrowthValueMapper, YxGrowthValue> implements IYxGrowthValueService {

    @Autowired
    private IYxUserService yxUserService;

    @SneakyThrows
    @Override
    public Boolean growthValueCalculation(PayDto payDto) {
        BigDecimal growthValue = null;
        //查询当前除法成长值规定
        YxGrowthValue yxGrowthValue = this.getById(payDto.getValueId());
        if (yxGrowthValue == null) {
            return Boolean.FALSE;
        }
        if (payDto.getActualPrice().compareTo(BigDecimal.valueOf(0)) > 0) {
            if (payDto.getIsCoupon().equals(0) && payDto.getActivityPrice() != null) {
                BigDecimal added = payDto.getActualPrice().add(payDto.getActivityPrice());
                growthValue = added.divide(yxGrowthValue.getGrowthPrice(), BigDecimal.ROUND_CEILING).multiply(yxGrowthValue.getGrowthValue()).setScale(2, 4);
            } else {
                growthValue = payDto.getActualPrice().divide(yxGrowthValue.getGrowthPrice()).multiply(yxGrowthValue.getGrowthValue()).setScale(2, 4);
            }
        }
        //0原支付的时候
        if (payDto.getActualPrice().compareTo(BigDecimal.valueOf(0)) == 0) {
            if (payDto.getTotalPrice() != null && payDto.getTotalPrice().compareTo(BigDecimal.ZERO) > 0) {
                growthValue = payDto.getTotalPrice().divide(yxGrowthValue.getGrowthPrice(), BigDecimal.ROUND_CEILING).multiply(yxGrowthValue.getGrowthValue()).setScale(2, 4);
            }
        }
        payDto.setGrowthValues(growthValue);
        //创建记录
        yxUserService.saveUserLevelValue(payDto);
        return Boolean.TRUE;
    }
}
